using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media;

namespace TwoPageView3
{
    sealed class UserPreferenceIO
    {
        public static string fileName = "Tpv3UserPreference.xml";
        private UserPreferenceIO()
        {
        }

        public static UserPreference LoadFromFile()
        {
            Console.WriteLine("D: LoadFromFile() {0}", fileName);
            System.Xml.Serialization.XmlSerializer formatter = 
                new System.Xml.Serialization.XmlSerializer(typeof(UserPreference));
            System.IO.IsolatedStorage.IsolatedStorageFileStream settingsFile;

            UserPreference up = new UserPreference();
            try {
                settingsFile = new System.IO.IsolatedStorage.IsolatedStorageFileStream(fileName,
                    System.IO.FileMode.Open,
                    System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForDomain());
            } catch (System.IO.FileNotFoundException ex) {
                Console.WriteLine(ex);
                return up;
            }

            {
                byte[] buffer = new byte[settingsFile.Length];
                settingsFile.Read(buffer, 0, (int)settingsFile.Length);
                using (var stream = new System.IO.MemoryStream(buffer)) {
                    up = formatter.Deserialize(stream) as UserPreference;
                }
                buffer = null;
                settingsFile.Close();
            }
            bool bOK = UserPreference.CurrentVersion == up.Version;
            if ((int)ShortcutKey.Max != up.ShortcutKeys.Length) {
                bOK = false;
            } else {
                string[] shortcutKeys = up.ShortcutKeys;
                foreach (string s in shortcutKeys) {
                    if (0 == s.Length) {
                        bOK = false;
                    }
                }
            }
            if (!bOK) {
                Console.WriteLine("D: LoadFromFile() Default userPreference");
                return new UserPreference();
            }
            Console.WriteLine("D: LoadFromFile() Success");
            return up;
        }

        public static void SaveToFile(UserPreference userPreference)
        {
            Console.WriteLine("D: SaveToFile() {0}", fileName);
            System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(typeof(UserPreference));
            System.IO.IsolatedStorage.IsolatedStorageFileStream isfs =
                new System.IO.IsolatedStorage.IsolatedStorageFileStream(fileName,
                        System.IO.FileMode.Create,
                        System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForDomain());
            try {
                userPreference.Version = UserPreference.CurrentVersion;
                s.Serialize(isfs, userPreference);
            } catch (System.IO.IOException ex) {
                Console.WriteLine(ex.ToString());
            } finally {
                isfs.Close();
            }
        }
    }
}
